package org.ogv.usada.models;

import org.ogv.OGV;
import org.ogv.spritesheet.SpriteSheet;

import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Pot {

	private SpriteSheet 	_ss;
	private float			_initPosX;
	private float			_initPosY;
	private	float			_shakeTime;
	private Splash[]		_splashes;
	private Sound			_splashSound;
	private PotDef			_def;
	private boolean			_tintActive;
	
	public Pot() {
		//create spritesheet
		_ss 		= new SpriteSheet();
		_splashes	= new Splash[10];
		for(int i = 0; i < _splashes.length; ++i) {
			_splashes[i] = new Splash();
		}
	}
	
	public void init(PotDef def, int posX, int posY) {
		_def = def;
		
		_ss.init(def.spriteSheet);
		_ss.setPosition(
			_initPosX = posX - (_ss.getWidth() * 0.5f), 
			_initPosY = posY - (_ss.getHeight() * 0.5f)
		);
		_ss.setAnim(0);

		_shakeTime = 0;

		for(int i = 0; i < _splashes.length; ++i) {
			_splashes[i].enable(false);
		}
		
		_splashSound = OGV.asset.get(def.splashSound, Sound.class);
		
	}
	
	public void destroy() {
		
	}
	
	public void update(float delta) {
		_ss.update(delta);
		
		
		if(_shakeTime > 0) {
			_shakeTime -= delta;
			if(_tintActive) _ss.setColor(_def.failTint);
			_ss.setPosition(
				_initPosX + GP.$().RANDOM.nextFloat() * 10f - 5f, 
				_initPosY + GP.$().RANDOM.nextFloat() * 10f - 5f
			);
		} else {
			_ss.setColor(Color.WHITE);
			_shakeTime -= delta;
			_ss.setPosition(_initPosX, _initPosY);
		}
		
		for(Splash s : _splashes) s.update(delta);
	}
	
	public void draw(SpriteBatch sb) {
		_ss.draw(sb);

		if(GP.$().DEBUG_DRAW) _def.mouthArea.draw(sb, _ss.getX(), _ss.getY());
		
		for(Splash s : _splashes) s.draw(sb);
	}
	
	public void shakePot(float dur, boolean useTint) {
		_shakeTime = dur;
		_tintActive= useTint;
	}
	
	public void makeSplash(float x, float y) {
		for(Splash s : _splashes) {
			if(!s.enabled()) {
				s.init(x, y, GP.$().GAME_POT_SPLASH_LIFETIME);
				_splashSound.play(GP.$().SOUND_VOL);
				break;
			}
		}
	}
	
	public boolean isPointOnTopFromMe(float y) {
		return y > _ss.getY() + _def.mouthArea.y;
	}
	
	public float getRandMouthPosY() { return _ss.getY() + _def.mouthArea.y + GP.$().RANDOM.nextFloat() * _def.mouthArea.height; }
	public float getRandMouthPosX() { return _ss.getX() + _def.mouthArea.x + GP.$().RANDOM.nextFloat() * _def.mouthArea.width; }
}
